// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.commons.utils;

import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;

/**
 * 
 * Utility class for strings. <br/>
 * 
 * $Id$
 * 
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-5-27 qiang.zhang $
 * 
 */
public class StringUtils {

    private static final String VALIDATE_PATTERN_COLUMN_NAME = "^[a-zA-Z_][a-zA-Z_0-9]*$"; //$NON-NLS-1$

    /**
     * Validates column name to avoid special characters.
     * 
     * @param columnName
     * @param index
     */
    public static String validateColumnNameFormat(String columnName, int index) {
        org.apache.oro.text.regex.Pattern validPatternColumnNameRegexp = null;
        PatternCompiler compiler = new Perl5Compiler();

        if (validPatternColumnNameRegexp == null) {
            try {
                validPatternColumnNameRegexp = compiler.compile(VALIDATE_PATTERN_COLUMN_NAME);
            } catch (MalformedPatternException e) {
                throw new RuntimeException(e);
            }
        }
        Perl5Matcher matcher = new Perl5Matcher();
        boolean match = matcher.matches(columnName, validPatternColumnNameRegexp);
        if (!match) {
            columnName = "Column" + index;
        }
        return columnName;
    }

    public static String repeat(String str, int repeat) {
        return org.apache.commons.lang.StringUtils.repeat(str, repeat);
    }

    public static String join(Object[] array, String separator) {
        return org.apache.commons.lang.StringUtils.join(array, separator);
    }

    public static String capitalize(String str) {
        return org.apache.commons.lang.StringUtils.capitalize(str);
    }

    /**
     * Extract string between the first delimiter and the second delimiter.
     * 
     * @param text
     * @param delimiter
     * @return
     */
    public static String extractFirstDelimitedString(String text, String delimiter) {

        String returned = ""; //$NON-NLS-1$

        int start = text.indexOf(delimiter, 0);

        if (start != -1) {

            int end = text.indexOf(delimiter, start + 1);

            if (end != -1) {
                returned = text.substring(start + 1, end);
            }
        }

        return returned;
    }

    public static String removeSpecialCharsForPackage(String input) {
        input = input.replaceAll(" ", ""); //$NON-NLS-1$ //$NON-NLS-2$
        input = input.replaceAll("/", "."); //$NON-NLS-1$ //$NON-NLS-2$
        input = input.replaceAll("&", "and"); //$NON-NLS-1$ //$NON-NLS-2$
        input = input.replaceAll("<", "lt"); //$NON-NLS-1$ //$NON-NLS-2$
        input = input.replaceAll(">", "gt"); //$NON-NLS-1$ //$NON-NLS-2$
        input = input.replaceAll("'", "apos"); //$NON-NLS-1$ //$NON-NLS-2$
        input = input.replaceAll("\"", "quot"); //$NON-NLS-1$ //$NON-NLS-2$
        return input;
    }

    /**
     * Reports if a string is blank. A string is considered blank either if it is null, is an empty string, of consists
     * entirely of white space.
     * <p>
     * For example,
     * <ul>
     * <li>null, "" and " " are blank strings
     * </ul>
     * 
     * @param str the string to check
     * @return true if the string is blank, false otherwise.
     */
    public static boolean isBlank(String str) {
        return trimString(str) == null;
    }

    /**
     * Trim a string. Removes leading and trailing blanks. If the resulting string is empty, normalizes the string to an
     * null string.
     * 
     * @param value the string to trim
     * @return the trimmed string, or null if the string is empty
     */

    public static String trimString(String value) {
        if (value == null)
            return null;
        value = value.trim();
        if (value.length() == 0)
            return null;
        return value;
    }

    /**
     * Convert an integer to an HTML RGB value. The result is of the form #hhhhhh. The input rgb integer value will be
     * clipped into the range 0 ~ 0xFFFFFF
     * 
     * @param rgb the integer RGB value
     * @return the value as an HTML RGB string
     */

    public static String toRgbText(int rgb) {
        // clip input value.

        if (rgb > 0xFFFFFF)
            rgb = 0xFFFFFF;
        if (rgb < 0)
            rgb = 0;

        String str = "000000" + Integer.toHexString(rgb); //$NON-NLS-1$ 
        return "#" + str.substring(str.length() - 6); //$NON-NLS-1$ 
    }

}
